import React from "react";
import { Navigate, RouteObject, useRoutes } from "react-router-dom";
import { getToken } from "../services/auth";
import LoginPage from "../pages/Login";
import ChatPage from "../pages/Chat";
import SocialLoginCallback from "../pages/Login/SocialLoginCallback";

/**
 * @description 路由守卫组件
 */
const PrivateRoute: React.FC<{ children: React.ReactNode }> = ({
  children,
}) => {
  const token = getToken();

  if (!token) {
    return <Navigate to="/login" replace />;
  }

  return <>{children}</>;
};

/**
 * @description 路由配置
 */
const routes: RouteObject[] = [
  {
    path: "/",
    element: <Navigate to="/chat" replace />,
  },
  {
    path: "/login",
    element: <LoginPage />,
  },
  {
    path: "/chat",
    element: <ChatPage />,
  },
  {
    path: "/auth/wechat/callback",
    element: <SocialLoginCallback />,
  },
  {
    path: "/auth/alipay/callback",
    element: <SocialLoginCallback />,
  },
  {
    path: "*",
    element: <Navigate to="/chat" replace />,
  },
];

/**
 * @description 路由组件
 */
const Router: React.FC = () => {
  const element = useRoutes(routes);
  return element;
};

export default Router;
